//
//  NSObject+TLNotification.m
//  TLLayout
//
//  Created by Tianlong on 2017/9/15.
//  Copyright © 2017年 Tianlong. All rights reserved.
//

#import "NSObject+TLNotification.h"
#import <objc/runtime.h>
#import <objc/objc.h>
#define TLNOTIFICATIONKEY @"TLNotificationKey"

@implementation NSObject (TLNotification)

-(void)addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject{
    NSString *noteKey = [NSString stringWithFormat:@"%@_%@",aName,TLNOTIFICATIONKEY];
    TLNotification *tlNote = objc_getAssociatedObject(self, &noteKey);
    if (tlNote) {
        return;
    }
    tlNote = [TLNotification addObserver:observer selector:aSelector name:aName object:anObject];
    objc_setAssociatedObject(self, &noteKey, tlNote, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)addObserver:(id)observer name:(NSNotificationName)aName object:(id)anObject callBackBlock:(CallBackBlock)callBack{
    NSString *noteKey = [NSString stringWithFormat:@"%@_%@",aName,TLNOTIFICATIONKEY];
    TLNotification *tlNote = objc_getAssociatedObject(self, &noteKey);
    if (tlNote) {
        return;
    }
    tlNote = [TLNotification addObserver:observer name:aName object:anObject callBackBlock:callBack];
    objc_setAssociatedObject(self, &noteKey, tlNote, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
